package com.wjq.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wjq.entity.User;
import com.wjq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@CrossOrigin
@RestController
public class LoginController {

    @Autowired
    UserService userService;

    @GetMapping("/")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/index")
    public String toindex(){
        return "index";
    }

    @GetMapping("/login")
    public String login(String userName,String password){
        if(userName == "admin" && password == "admin")
            return "success";
        return null;
    }

    @RequestMapping("verifyLogin")
    public String verifyLogin(String userName, HttpServletResponse response) throws JsonProcessingException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        User user = userService.selectUserByUsername(userName);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(user);
        System.out.println(s);
        return s;
    }
}
